package tw.com.softleader.reins.demo.book.service;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import tw.com.softleader.reins.demo.book.entity.Book;

public class BookService {
	private static final String SELECT_ALL = "SELECT * FROM public.bookdb ORDER BY id";
	private static final String SELECT_BY_ID = "SELECT * FROM public.bookdb WHERE id = ?";
	private static final String INSERT_FULL = "INSERT INTO public.bookdb VALUES (default,?,?,now(),?)";
	
	private Connection connection = null;
	
	private void getConnection(){
		try {
			Class.forName("org.postgresql.Driver");
			connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/reinsDB","postgres", "postgres");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	public List<Book> getAll(){
		List<Book> books = new ArrayList<Book>();
		getConnection();
		
		try {
			PreparedStatement pstmt = connection.prepareStatement(SELECT_ALL);
			ResultSet rs = pstmt.executeQuery();
			while(rs.next()){
				books.add(new Book(rs.getLong(1), rs.getString(2), rs.getString(3), rs.getTimestamp(4), rs.getString(5)));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return books;
	}
	
	public Book getById(Integer id){
		Book book = null;
		getConnection();
		
		try {
			PreparedStatement pstmt = connection.prepareStatement(SELECT_BY_ID);
			pstmt.setInt(1, id);
			ResultSet rs = pstmt.executeQuery();
			while(rs.next()){
				book = new Book(rs.getLong(1), rs.getString(2), rs.getString(3), rs.getTimestamp(4), rs.getString(5));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return book;

	}
	
	public int insertFullData(Book book){
		getConnection();
		int result = 0;
		try {
			PreparedStatement pstmt = connection.prepareStatement(INSERT_FULL);
			pstmt.setString(1, book.getTitle());
			pstmt.setString(2, book.getWriter());
			pstmt.setString(3, book.getIsbn());
			result = pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return result;
	}
	
	public int insertFullDataBatch(List<Book> books){
		getConnection();
		try {
			connection.setAutoCommit(false);
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		int result = 0;
		try {
			PreparedStatement pstmt = connection.prepareStatement(INSERT_FULL);
			for(Book book:books){
				pstmt.setString(1, book.getTitle());
				pstmt.setString(2, book.getWriter());
				pstmt.setString(3, book.getIsbn());
				pstmt.addBatch();
			}
			pstmt.executeBatch();
			connection.commit();
			pstmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println(e.getNextException());
		} finally {
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return result;
	}
	
}
